package juc;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 生产者 消费者
 *
 * 多线程交互中 一定要防止虚假唤醒(判断只能用while 不能用if)
 *
 */

class AirCon{
    private int number = 0;

    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();

    public void increment() throws InterruptedException {


        lock.lock();
        try {
            while (number != 0){
                condition.await();//this.wait();
            }
            number++;
            System.out.println(Thread.currentThread().getName() + "\t" + number);

            condition.signalAll();//this.notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

    public void decrement() throws InterruptedException {


        lock.lock();
        try {
            while (number == 0){
                condition.await();//this.wait();
            }
            number--;
            System.out.println(Thread.currentThread().getName() + "\t" + number);

            condition.signalAll();//this.notifyAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }

    }

//    public synchronized void increment() throws InterruptedException {
////        if (number != 0){
////            this.wait();
////        }
//        while (number != 0){
//            this.wait();
//        }
//        number++;
//        System.out.println(Thread.currentThread().getName() + "\t" + number);
//
//        this.notifyAll();
//    }
//
//    public synchronized void decrement() throws InterruptedException {
//        while (number == 0){
//            this.wait();
//        }
//        number--;
//        System.out.println(Thread.currentThread().getName() + "\t" + number);
//
//        this.notifyAll();
//    }


}

/**
 * 两个线程对一个初始值为0的值操作 加一 减一 10此后 值为0
 *
 */
public class ThreadWaitNotifyDemo {
    public static void main(String args[]){
        AirCon airCon = new AirCon();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCon.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"A").start();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCon.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"B").start();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCon.increment();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"C").start();


        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                try {
                    airCon.decrement();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"D").start();
    }
}
